package com.ruoyi.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.FileLibraryModel;
import com.ruoyi.system.domain.dto.DeleteDto;
import com.ruoyi.system.domain.dto.QueryDto;
import com.ruoyi.system.service.FileLibraryService;
import lombok.Data;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author ：Wangziyi
 * @date ：Created in 2023/12/12 10:21
 * @description：ToDo
 * @modified By：
 * @version: 1.0
 */
@RestController
@RequestMapping("/file-library")
public class FileLibraryController {

    @Autowired
    private FileLibraryService fileLibraryService;

    @PostMapping("/add")
    public R add(@RequestBody FileLibraryModel fileLibraryModel){

        if (fileLibraryModel.getLibName() == null){
            return R.fail("必须提供知识库名称");
        }

        fileLibraryModel.setUserId(SecurityUtils.getUserId());
        fileLibraryModel.setCreator(SecurityUtils.getUserId());
        fileLibraryModel.setGmtCreated(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        fileLibraryModel.setIsPublic(1);
        fileLibraryModel.setIsDeleted(0);

        boolean res = fileLibraryService.save(fileLibraryModel);
        if (res){
            return R.ok(null,"新增成功！");
        }else {
            return R.fail("保存失败！");
        }
    }

    @PostMapping("/list")
    public R list(@RequestBody QueryDto queryDto){
        if (queryDto.getPage() == null || queryDto.getPage() == 0){
            queryDto.setPage(1L);
        }
        if (queryDto.getSize() == null || queryDto.getSize() == 0){
            queryDto.setSize(10L);
        }
        Page<FileLibraryModel> page = new Page<>(queryDto.getPage(), queryDto.getSize());
        LambdaQueryWrapper<FileLibraryModel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FileLibraryModel::getIsDeleted,0);
        queryWrapper.eq(FileLibraryModel::getUserId,SecurityUtils.getUserId());
        queryWrapper.like(FileLibraryModel::getLibName,queryDto.getLibName());
        queryWrapper.apply("lib_name NOT LIKE {0}", "dev-cx-temp%");
        Page<FileLibraryModel> pageResult = fileLibraryService.page(page, queryWrapper);
        return R.ok(pageResult);
    }

    @PreAuthorize("@ss.hasPermi('admin')")
    @PostMapping("/list-public")
    public R listPublic(@RequestBody QueryDto queryDto){
        if (queryDto.getPage() == null || queryDto.getPage() == 0){
            queryDto.setPage(1L);
        }
        if (queryDto.getSize() == null || queryDto.getSize() == 0){
            queryDto.setSize(10L);
        }
        Page<FileLibraryModel> page = new Page<>(queryDto.getPage(), queryDto.getSize());
        LambdaQueryWrapper<FileLibraryModel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FileLibraryModel::getIsDeleted,0);
        queryWrapper.eq(FileLibraryModel::getIsPublic,0);
        Page<FileLibraryModel> pageResult = fileLibraryService.page(page, queryWrapper);
        return R.ok(pageResult);
    }

    @PostMapping("/update")
    public R update(@RequestBody FileLibraryModel fileLibraryModel){
        if (fileLibraryModel.getId() == null){
            return R.fail("必须提供文档库id");
        }

        checkPublicPermission(fileLibraryModel.getId());

        boolean res = fileLibraryService.updateById(fileLibraryModel);
        if (res){
            return R.ok(null,"修改成功！");
        }else {
            return R.fail("修改失败！");
        }
    }

    @PostMapping("/delete")
    public R delete(@RequestBody DeleteDto deleteDto){
        if (deleteDto.getId() == null){
            return R.fail("必须提供文档库id");
        }

        checkPublicPermission(deleteDto.getId());

        boolean res = fileLibraryService.removeById(deleteDto.getId());
        if (res){
            return R.ok(null,"删除成功！");
        }else {
            return R.fail("删除失败！");
        }
    }

    @PostMapping("/logic-delete")
    public R logicDelete(@RequestBody DeleteDto deleteDto){
        if (deleteDto.getId() == null){
            return R.fail("必须提供文档库id");
        }

        checkPublicPermission(deleteDto.getId());

        Boolean res = fileLibraryService.logicRemoveFileLibrary(deleteDto.getId());
        if (res){
            return R.ok(null,"删除成功！");
        }else {
            return R.fail("删除失败！");
        }
    }

    private void checkPublicPermission(Long id){
        if (id == 1){
            if (!SecurityUtils.getRoles().contains("超级管理员")){
                throw new ServiceException("对不起，您没有权限进行此操作操作");
            }
        }
    }


}
